package com.b2b.util;


import com.b2b.model.CompanyUserRoleDto;
import com.b2b.model.UserDto;
import com.b2b.model.common.Constant;
import com.b2b.model.entity.User;

import com.b2b.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 获取当前登录用户工具类
 *
 * @author wliduo[i@dolyw.com]
 * @date 2019/3/15 11:45
 */
@Component
public class UserUtil {

    private final UserService userService;
    private final CompanyUserRoleUtil companyUserRoleUtil;
    private final CompanyInfoUtil companyInfoUtil;

    @Autowired
    public UserUtil(UserService userService,CompanyUserRoleUtil companyUserRoleUtil,CompanyInfoUtil companyInfoUtil) {
        this.userService = userService;
        this.companyUserRoleUtil = companyUserRoleUtil;
        this.companyInfoUtil = companyInfoUtil;
    }


    public List<User> getUserByCompanyId(Integer companyId){
        List<User> userList = new ArrayList<>();
        List<Integer> userIds = companyUserRoleUtil.getUserList(companyId);
        for (Integer value:userIds){
            User user = getUser(value);
            userList.add(user);
        }
        return userList;
    }
    /**
     * 获取当前登录用户
     *
     * @param
     * @return com.wang.model.UserDto
     * @author wliduo[i@dolyw.com]
     * @date 2019/3/15 11:48
     */
    public User getUser(Integer userId) {
//        String token = SecurityUtils.getSubject().getPrincipal().toString();
//        // 解密获得Account
//        String account = JwtUtil.getClaim(token, Constant.ACCOUNT);
        User user = new User();
        user.setUserId(userId);
        user = userService.selectByPrimaryKey(user);
        return user;
    }

    public User getUserByName(String userName){
        User user = new User();
        user.setUserName(userName);
        user = userService.selectOne(user);
        return user;
    }
    public List<User> getAll(){
        List<User> userList = userService.selectAll();
        return userList;
    }

    public Integer insertNewUser(User user){
        Integer companyId = companyInfoUtil.insertCompany(user);
        if (companyId <=0 ){
            return companyId;
        }else {
            user.setRegTime(new Date());
            user.setState('0');
            userService.insert(user);
            Integer userId = user.getUserId();
            Integer result = companyUserRoleUtil.addNewUser(userId,companyId);
            return result;
        }
    }
    /**
     * 获取当前登录用户企业Id
     *
     * @param
     * @return com.wang.model.UserDto
     * @author wliduo[i@dolyw.com]
     * @date 2019/3/15 11:48
     */
//    public Integer getCompanyId(Integer Id) {
//        CompanyUserRoleDto companyUserRoleDto = new CompanyUserRoleDto();
//        companyUserRoleDto.setUserId(Id);
//        companyUserRoleDto = userMapper.selectOne(companyUserRoleDto);
//    }

    /**
     * 获取当前登录用户Token
     *
     * @param
     * @return com.wang.model.UserDto
     * @author wliduo[i@dolyw.com]
     * @date 2019/3/15 11:48
     */
    public String getToken() {
        return SecurityUtils.getSubject().getPrincipal().toString();
    }

    /**
     * 获取当前登录用户Account
     *
     * @param
     * @return com.wang.model.UserDto
     * @author wliduo[i@dolyw.com]
     * @date 2019/3/15 11:48
     */
    public String getAccount() {
        String token = SecurityUtils.getSubject().getPrincipal().toString();
        // 解密获得Account
        return JwtUtil.getClaim(token, Constant.PHONE);
    }
    public Integer addEmployee(Map<String,String> user){
        String userName = user.get("userName");
        String phone = user.get("phone");
        String password = user.get("password");
        Integer companyId = Integer.parseInt(user.get("companyId"));
        Integer roleId = Integer.parseInt(user.get("roleId"));
        User user1 = new User();
        user1.setUserName(userName);
        user1.setPhone(phone);
        user1.setPassword(password);
        Integer count = insertUser(user1);
        if (count<=0){
            return count;
        }
        Integer userId = user1.getUserId();
        CompanyUserRoleDto companyUserRoleDto = new CompanyUserRoleDto();
        companyUserRoleDto.setUserId(userId);
        companyUserRoleDto.setRoleId(roleId);
        companyUserRoleDto.setCompanyId(companyId);
        Integer count1 = companyUserRoleUtil.addCompanyUserRoleDto(companyUserRoleDto);
        if (count1 <= 0) {
            return count1;
        }else {
            return userId;
        }
    }
    public Integer deleteEmployee(Integer userId){
        Integer count = userService.deleteByPrimaryKey(userId);
        if (count >= 0) {
            count = companyUserRoleUtil.deleteCompanyUserRoleDto(userId);
            return count;
        }else {
            return count;
        }
    }
    public String getUserName(Integer userId){
        User user = new User();
        user.setUserId(userId);
        user = userService.selectByPrimaryKey(user);
        String userName = user.getUserName();
        return userName;
    }

    public Integer insertUser(User user){
        Integer count = userService.insert(user);
        if (count<=0){
            return count;
        }else {
            count = user.getUserId();
            return count;
        }
    }
    public User getBaseUser(UserDto userDto){
        User user = new User();
        user.setUserId(userDto.getUserId());
        user.setUserName(userDto.getUserName());
        user.setImgUrl(userDto.getImgUrl());
        return user;
    }
    public User getUser(UserDto userDto){
        User user = new User();
        user.setUserId(userDto.getUserId());
        user.setUserName(userDto.getUserName());
        user.setImgUrl(userDto.getImgUrl());
        user.setPassword(userDto.getPassword());
        user.setPhone(userDto.getPhone());
        user.setRegTime(userDto.getRegTime());
        return user;
    }
}
